package com.gali.lovefinder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gali.lovefinder.common.ErrorCode;
import com.gali.lovefinder.constant.ReportStatusEnum;
import com.gali.lovefinder.exception.BusinessException;
import com.gali.lovefinder.mapper.ReportMapper;
import com.gali.lovefinder.model.bo.report.AddReportBO;
import com.gali.lovefinder.model.entity.Report;
import com.gali.lovefinder.service.ReportService;
import com.gali.lovefinder.utils.GsonUtil;
import com.gali.lovefinder.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author gali
 * @description 针对表【report(举报)】的数据库操作Service实现
 * @createDate 2023-07-03 23:05:04
 */
@Service
@Slf4j
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report>
        implements ReportService {

    @Override
    public void addReport(AddReportBO addReportBO) {
        Report report = new Report();
        report.setContent(addReportBO.getContent());
        report.setReportedId(addReportBO.getReportedId());
        report.setReportedUserId(addReportBO.getReportedUserId());
        report.setStatus(ReportStatusEnum.NOT_APPROVE.getStatus());
        report.setUserId(UserInfoUtil.getUserId());
        boolean save = this.save(report);
        if (!save) {
            log.error("report save failed,report:{}", GsonUtil.toJson(report));
            throw new BusinessException(ErrorCode.SAVE_ERROR);
        }
    }

    @Override
    public void approveReport(String reportId) {
        Report report = new Report();
        report.setId(Long.valueOf(reportId));
        report.setStatus(ReportStatusEnum.APPROVE.getStatus());
        boolean b = this.updateById(report);
        if (!b) {
            log.error("report update failed,report:{}", GsonUtil.toJson(report));
            throw new BusinessException(ErrorCode.UPDATE_ERROR);
        }
    }
}




